<template>
  <div class="view-fill">
    <div class="app-container-flex">
      <NewFullScreen
        v-model="showDialog"
        title="仓库智能柜预警详情"
        @returnBack="returnBack"
      >
        <div>
          <!-- 条件栏 -->
          <div class="filter-wrapper">
            <new-page-filter
              v-model:query="filterInfo.query.queryMap"
              :query-more-json="true"
              :filter-list="filterInfo.searchList"
              :filter-btn="filterInfo.searchBtn"
              :clear-type="clearType"
              :slipSelectRequest="slipSelectRequest"
            >
            </new-page-filter>
          </div>
          <div class="filter-wrapper">
            <div>
              <modelTips
                :items="modelTipsInfo.items"
                :title="modelTipsInfo.title"
                :brackets="true"
              />
            </div>
            <div class="display-flex">
              <el-button @click="preferenceInfo.visible = true"
                >偏好设置</el-button
              >
            </div>
          </div>
          <!-- 表格 -->
          <TableContainer
            :refresh="tableInfo.refresh"
            :init-curpage="tableInfo.initCurpage"
            v-model:data="tableInfo.data"
            :api="tableInfo.api"
            :query="filterInfo.query"
            :field-list="tableInfo.fieldList"
            :checkBox="false"
            :tabIndex="true"
            :row-class-name="tableRowClassName"
          >
          </TableContainer>
        </div>
      </NewFullScreen>
    </div>
    <SetPreference
      v-if="preferenceInfo.visible"
      v-model:visible="preferenceInfo.visible"
      :search-list="filterInfo.searchList"
      :field-list="tableInfo.fieldList"
      :customize-user-v-o="filterInfo.query.customizeUserVO"
      :group-level-show="false"
      @callback="setPreferenceCallback"
    />
    <AdvancedSearch
      v-if="advancedSearchInfo.visible"
      v-model:visible="advancedSearchInfo.visible"
      v-model:form-data="advancedSearchInfo.data"
      :field-list="tableInfo.fieldList"
      @callback="advancedSearchCallback"
    />
  </div>
</template>
<script setup name="stockWaringSummaryDetails">
import { getStockWaringCustomizeListPage } from '@/api/smartCabinet'
import { getCustomizeSetting, updateUserCustomize } from '@/api/customize'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  smartCabinetDepotId: {
    type: Number,
  },
})
//颜色标识
const modelTipsInfo = ref({
  title: '类型',
  items: [
    {
      label: '高水位',
      style: { width: '10px', height: '10px', background: '#fceacb' },
    },
    {
      label: '低水位',
      style: { width: '10px', height: '10px', background: '#f5e4e4' },
    },
  ],
})
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {
      smartCabinetDepotId: {
        queryType: 'IN',
        value: props.smartCabinetDepotId,
      },
    },
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: _search },
    { type: 'button', label: '高级查询', event: _advancedSearchShow },
    { type: 'button', label: '重置', event: _clearSearch },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [],
})
const showDialog = computed({
  get() {
    return props.visible
  },
  set(val) {
    // 更新父组件visible
    emits('update:visible', val)
  },
})
setData()
function setData() {
  let obj = {
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}

const returnBack = () => {
  emits('returnBack')
}
function _search() {
  search()
}
function _advancedSearchShow() {
  advancedSearchShow()
}
function _clearSearch() {
  clearSearch()
}

const preferenceInfo = ref({
  visible: false,
})
const advancedSearchInfo = ref({
  visible: false,
  data: {},
})
const clearType = ref(true)
const slipSelectRequest = ref(true)

function setPreferenceCallback(obj) {
  updateUserCustomize(obj.customizeUserVO).then((res) => {
    if (res && res.success) {
      getCustomize(!slipSelectRequest.value)
    }
  })
}
function advancedSearchShow() {
  advancedSearchInfo.value.visible = true
}
// 获取列表
async function getList() {
  tableInfo.value.refresh = Math.random()
}
function tableRowClassName({ row = undefined }) {
  let rowId = sessionStorage.getItem('storeRowId')
  if (rowId && JSON.stringify(row) === rowId) {
    return 'check-row'
  } else if (row.qty > row.highLevel) {
    return 'nearly-row'
  } else if (row.qty <= row.lowLevel) {
    return 'expire-row'
  }
  return ''
}
// 搜索
function search() {
  // 重置分页
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryDtoList = []
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  clearType.value = !clearType.value
  advancedSearchInfo.value.data = {}
  filterInfo.value.query.queryMap = proxy.$fn.deepClone(
    filterInfo.value.copyQueryMap
  )
  search()
}

async function advancedSearchCallback(obj) {
  let _filterInfo = filterInfo.value
  _filterInfo.query.queryMap = {}
  let arr = []
  _filterInfo.query.queryDtoList = [...obj.queryDtoList, ...arr]
  advancedSearchInfo.value.data = obj.data
  await getList()
  clearType.value = !clearType.value
  _filterInfo.query.queryMap = proxy.$fn.deepClone(_filterInfo.copyQueryMap)
}

function getCustomize(_slipSelectRequest = true) {
  let _tableInfo = tableInfo.value
  let _filterInfo = filterInfo.value
  let param = {
    tableKey:
      'smartCabinetStockWaring-com.slip.spd.entity.vo.smartCabinet.SmartCabinetStockWaringVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        _filterInfo.searchList,
        data.detail
      )
      _filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ..._filterInfo.query.queryMap,
      }
      _filterInfo.searchList = searchInfo.searchList
      _filterInfo.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.value.copyQueryMap = proxy.$fn.deepClone(
        filterInfo.value.query.queryMap
      )
      slipSelectRequest.value = _slipSelectRequest
      _tableInfo.api = getStockWaringCustomizeListPage
      getList()
    }
  })
}
</script>

<style scoped lang="scss">
:deep(.el-table) {
  .nearly-row {
    background: #fceacb;
  }
  .expire-row {
    background: #f5e4e4;
  }
}
</style>
